﻿

function addRecordType()
{
    var src = "/User/AddRecordType.aspx";
    $("#detailFrame").attr("src", src);
}

function viewRecordType(node)
{
    var bar = $(node.parentNode).next();
    var idInput = bar.find("input[name=id]");
    var src = "/User/ViewRecordType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function editRecordType(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/User/EditRecordType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function removeRecordType(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/User/RemoveRecordType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}



////////////////////////////

function renameTreeNode(e, data)
{
    $.post(
		"/Record/AjaxRecord.aspx", 
		{ 
			"operation" : "rename_node", 
			"id" : data.rslt.obj.attr("id").replace("node_",""),
			"title" : data.rslt.new_name
		}, 
		function (r) {
			if(!r.status) {
				$.jstree.rollback(data.rlbk);
				if(r.error != "")
				{
				    alert(r.error);
				}
			}
		}
	);
}

function removeTreeNode(e, data)
{
    data.rslt.obj.each(function () {
	    $.ajax({
		    async : false,
		    type: 'POST',
		    url: "/Record/AjaxRecord.aspx",
		    data : { 
			    "operation" : "remove_node", 
			    "id" : this.id.replace("node_","")
		    }, 
		    success : function (r) {
			    if(!r.status) {
				    $.jstree.rollback(data.rlbk);
				    if(r.error != "")
			        {
			            alert(r.error);
			        }
			    }
		    }
	    });
    });
}

function createTreeNode(e, data)
{
    $.post(
		"/Record/AjaxRecord.aspx", 
		{ 
			"operation" : "create_node", 
			"id" : data.rslt.parent.attr("id").replace("node_",""), 
			"position" : data.rslt.position,
			"title" : data.rslt.name,
			"type" : data.rslt.obj.attr("rel")
		}, 
		function (r) {
		    if(r.status) {
				$(data.rslt.obj).attr("id", r.id);
			}
			else {
				$.jstree.rollback(data.rlbk);
				if(r.error != "")
				{
				    alert(r.error);
				}
			}
		}
	);
}

function moveTreeNode(e, data)
{
    data.rslt.o.each(function (i) {
		$.ajax({
			async : false,
			type: 'POST',
			url: "/Record/AjaxRecord.aspx",
			data : { 
				"operation" : "move_node", 
				"id" : $(this).attr("id").replace("node_",""), 
				"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""), 
				"position" : data.rslt.cp + i,
				"title" : data.rslt.name,
				"copy" : data.rslt.cy ? 1 : 0
			},
			success : function (r) {
				if(!r.status) {
					$.jstree.rollback(data.rlbk);
					if(r.error != "")
					{
					    alert(r.error);
					}
				}
				else {
					$(data.rslt.oc).attr("id", "node_" + r.id);
					if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
						data.inst.refresh(data.inst._get_parent(data.rslt.oc));
					}
				}
				$("#analyze").click();
			}
		});
	});
}

function selectTreeNode(recordTypeId)
{
    if (recordTypeId == emptyId)
    {
        $("#divRecordType").css("display", "none");
        return;
    }
    $("#divRecordType").css("display", "inline");
    var area = mask();
    $.post(
		"/Record/AjaxRecord2.aspx", 
		{ 
			"action" : "select_node", 
			"recordTypeId" : recordTypeId
		}, 
		function (r) {
		    if (r.status)
		    {
		        var tableRecord = $("#divRecord");
		        tableRecord.find(".header").text(r.recordType.name);
		        var txaContent = tableRecord.find("textarea");
		        var content = r.recordType.content;
		        while (content.indexOf("<br/>") >= 0)
		        {
		            content = content.replace("<br/>", "\n");
		        }
		        txaContent.val("");
		        txaContent.val(content);
		        var divFiles = tableRecord.find(".cell").eq(1);
		        removeAllAttachments(divFiles);
		        for (var i=0; i<r.recordType.files.length; i++)
		        {
		            addAttachment(divFiles, r.recordType.files[i].id, r.recordType.files[i].dfile, r.recordType.files[i].title);
		        }
		/*
		    var table = $(".div-table");
	        var rows = table.find(".div-row");
	        for (var j=rows.length-2; j>0; j--)
	        {
                rows.eq(j).remove();
	        }
	        var header = rows.eq(0);
	        var fm = createFormat(r.assets.length);
		    for (var i=r.assets.length-1; i>=0; i--)
		    {
		        var newRow = "<div class=\"div-row\">";
		        newRow += "<div class=\"div-cell c0\">"+r.assets[i].order+"</div>";
                newRow += "<div class=\"div-cell c1\">";
                newRow += "<span class=\"edit\" onclick=\"editAsset(this, 'edit');\"></span>";
                newRow += "<span class=\"remove\" onclick=\"removeAsset(this);\"></span>";
                newRow += "<input type=\"hidden\" name=\"assetId\" value=\""+r.assets[i].assetId+"\" />";
                newRow += "<div>"+r.assets[i].assetName+"</div>";
                newRow += "</div>";
                newRow += "<div class=\"div-cell c4\">"+r.assets[i].description+"</div>";
                newRow += "<div class=\"div-cell c2\">"+r.assets[i].startDate+"</div>";
                newRow += "<div class=\"div-cell c2\">"+r.assets[i].endDate+"</div>";
                newRow += "</div>";
                $(newRow).insertAfter(header);
		    }*/
		    }
		}
	)
	.fail(function() { alert("error"); })
	.always(function() {
	    unmask(area);
	});
}

function updateRecordType()
{
    var tableRecord = $("#divRecord");
    tableRecord.find(".header").text(r.recordType.name);
    var txaContent = tableRecord.find("textarea");
    var content = txaContent.val();
    while (content.indexOf("\n") >= 0)
    {
        content = content.replace("\n", "<br/>");
    }
    
    var files = tableRecord.find(".attachment");
    var filesExp = "";
    for (var i=0; i<files.length; i++)
    {
        var generatedName = files.eq(i).find("a").attr("href");
        generatedName = generatedName.substring(generatedName.indexOf("=")+1, generatedName.length);
        var fileId = files.eq(i).find("input[name=attachmentId]").val();
        if (filesExp != "")
        {
            filesExp = ",";
        }
        filesExp += fileId + "|" + generatedName;
    }
    
    $.post(
		"/Record/AjaxRecord2.aspx", 
		{ 
			"operation" : "update_record_type", 
			"id" : data.rslt.obj.attr("id").replace("node_",""),
			"content" : content,
			"files" : filesExp
		}, 
		function (r) {
			if(!r.status) {
				$.jstree.rollback(data.rlbk);
				if(r.error != "")
				{
				    alert(r.error);
				}
			}
		}
	);
}

function editSetting(button, mode)
{
    var row = $(button).closest(".div-row");
    var cells = row.find(".div-cell");
    if (cells.eq(1).find("input[type=text]").length > 0)
    {
        processUpdate(button);
    }
    
    var divName = cells.eq(1).find("div");
    var input = "";
    if (mode == "add")
    {
        input += "<input type=\"hidden\" name=\"settingId\" value=\"\" />";
    }
    input += "<input type=\"text\" name=\"settingName\" value=\""+divName.text()+"\" onkeydown=\"enter(this, event);\"/>";
    if (divName.length > 0)
    {
        divName.eq(0).remove();
    }
    if (cells.eq(1).find("input[type=text]").length < 1)
    {
        cells.eq(1).append(input);
    }
    
    
    if (cells.eq(2).find("input[type=text]").length < 1)
    {
        var select = $("#divClone select");
        cells.eq(2).html(select.clone());
    }
    if (cells.eq(3).find("textarea").length < 1)
    {
        input = "<textarea name=\"inputValue\">"+cells.eq(3).text()+"</textarea>";
        //cells.eq(3).html(input);
    }
    
    if (mode=="add" || mode=="enter")
    {
        var table = row.parent();
        var newRow = "<div class=\"div-row\">";
        newRow += "<div class=\"div-cell c0\"></div>";
        newRow += "<div class=\"div-cell c1\">";
        newRow += "<span class=\"add\" onclick=\"editSetting(this, 'add');\"></span>";
        newRow += "<span class=\"remove\" onclick=\"removeSetting(this);\" style=\"visibility:hidden\"></span>";
        newRow += "<input type=\"hidden\" name=\"settingId\" value=\"\" />";
        newRow += "<div></div>";
        newRow += "</div>";
        newRow += "<div class=\"div-cell c2\"></div>";
        newRow += "<div class=\"div-cell c4\"></div>";
        newRow += "</div>";
        table.append(newRow);
        
        var addButton = cells.eq(1).find(".add");
        if (addButton.length > 0)
        {
            addButton.eq(0).attr("class", "edit");
            addButton.eq(0).attr("onclick", "editSetting(this, 'edit');");
        }
        var enterButton = cells.eq(1).find(".remove");
        if (enterButton.length > 0)
        {
            enterButton.eq(0).css("visibility", "visible");
        }
    }
    cells.eq(1).find("input[type=text]").focus();
}

function inputTypeChange(select)
{
    var row = $(select).closest(".div-row");
    var cells = row.find(".div-cell");
    var textarea = cells.eq(3).find("textarea");
    if ($(select).val() != "4")//input type = list
    {
        if (textarea.length > 0)
        {
            textarea.remove();
        }
        return;
    }
    if (textarea.length < 1)
    {
        var input = "<textarea name=\"inputValue\">"+cells.eq(3).text()+"</textarea>";
        cells.eq(3).html(input);
    }
}







